package bo;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;

import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

@Entity(name = "Team")
public class Team {
	@Id
	int teamId;
	
	@OneToMany(fetch = FetchType.LAZY, cascade=CascadeType.ALL, mappedBy="id.team")
	@Fetch(FetchMode.SELECT)
	Set<TeamSeason> seasons = new HashSet<TeamSeason>();
	
	@OneToMany(fetch = FetchType.LAZY, cascade=CascadeType.ALL, mappedBy="id.team")
	@Fetch(FetchMode.SELECT)
	Set<TeamSeasonPlayer> seasonPlayers = new HashSet<TeamSeasonPlayer>();
	
	@Column
	String name;
	@Column
	String league;
	@Column
	Integer yearFounded;
	@Column
	Integer yearLast;
	
	public TeamSeason getTeamSeason(Integer year) {
		for (TeamSeason ts : seasons) {
			if (ts.getYear() == year) return ts;
		}
		return null;
	}
	
	public void addSeason(TeamSeason s) {
		seasons.add(s);
	}
	public Set<TeamSeason> getSeasons() {
		return seasons;
	}
	public void setSeasons(Set<TeamSeason> seasons) {
		this.seasons = seasons;
	}
	
	public TeamSeasonPlayer getTeamSeasonPlayer(Integer year, Player p) {
		for (TeamSeasonPlayer ts : seasonPlayers) {
			if (ts.getYear() == year && ts.getPlayer().equals(p) ) return ts;
		}
		return null;
	}
	
	public Set<TeamSeasonPlayer> getTeamSeasonPlayerByYear(Integer year) {
		Set<TeamSeasonPlayer> set = new HashSet<TeamSeasonPlayer>();
		for (TeamSeasonPlayer tsp : seasonPlayers) {
			if (tsp.getYear() == year) set.add(tsp);
		}
		return set;
	}
	
	public void addSeasonPlayer(TeamSeasonPlayer s) {
		seasonPlayers.add(s);
	}
	public Set<TeamSeasonPlayer> getSeasonPlayer() {
		return seasonPlayers;
	}
	public void setSeasonPlayers(Set<TeamSeasonPlayer> seasons) {
		this.seasonPlayers = seasons;
	}
	
	public int getTeamId () {
		return this.teamId;
	}
	public void setTeamId (int id) {
		this.teamId = id;
	}
	
	public String getName () {
		return this.name;
	}
	public void setName (String name) {
		this.name = name;
	}
	
	public String getLeague () {
		return this.league;
	}
	public void setLeague (String league) {
		this.league = league;
	}
	
	public Integer getYearFounded () {
		return this.yearFounded;
	}
	public void setYearFounded (Integer year) {
		this.yearFounded = year;
	}
	
	public Integer getYearLast () {
		return this.yearLast;
	}
	public void setYearLast (Integer year) {
		this.yearLast = year;
	}
	
}
